<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @author hezhong
 */
class Car_fault_model extends CI_Model
{

    function select_car_fault_list($param = array(
        'faultid' => '',
        'faultname' => '',
        'start' => 0,
        'length' => 10))
    {
        if($param['faultid']) {
            $this->db->where('faultid',$param['faultid']);
        }

        if($param['faultname']) {
            $this->db->like('faultname',$param['faultname']);
        }

        $data['count'] = $this->db->count_all_results('car_fault');

        $this->db->from('car_fault');
        $this->db->join('car_fault_type', 'car_fault.typeid = car_fault_type.typeid');
        $this->db->order_by("faultid", "asc");
        $this->db->limit($param['length'], $param['start']);

        if($param['faultid']) {
            $this->db->where('faultid',$param['faultid']);
        }

        if($param['faultname']) {
            $this->db->like('faultname',$param['faultname']);
        }

        $query = $this->db->get();

        $data['list'] = $query->result_array();

        return $data;
    }


    function  select_car_fault_type_list()
    {
        $this->db->from('car_fault_type');
        $this->db->order_by("typeid", "asc");
        return $this->db->get()->result_array();
    }

    function insert()
    {
        $data = array(
            'typeid' => $this->input->post('typeid'),
            'faultname' => $this->input->post('faultname'),
            'faultinfo' => $this->input->post('faultinfo'),
            'faultreason' => $this->input->post('faultreason'),
            'faultsolve' => $this->input->post('faultsolve'),
            'faulttag' => $this->input->post('faulttag')
        );

        $this->db->insert('car_fault', $data);
    }

    function delete() {
        $faultid = $this->input->get_post('faultid');

        $this->db->where('faultid',$faultid);
        $this->db->delete('car_fault');
    }

    function select_by_id()
    {
        $faultid = $this->input->get_post('faultid');

        $this->db->where('faultid',$faultid);
        $query = $this->db->get('car_fault');
        $row = $query->row_array();
        return $row;
    }

    function update() {
        $faultid = $this->input->get_post('faultid');

        $fields = array(
            'typeid' => $this->input->post('typeid'),
            'faultname' => $this->input->post('faultname'),
            'faultinfo' => $this->input->post('faultinfo'),
            'faultreason' => $this->input->post('faultreason'),
            'faultsolve' => $this->input->post('faultsolve'),
            'faulttag' => $this->input->post('faulttag')
        );

        $this->db->where('faultid', $faultid);
        $this->db->update('car_fault', $fields);
    }

}